package org.autocode.code;

import org.autocode.code.type.JavaType;
import org.autocode.util.JavaUtils;
import org.autocode.util.ObjectUtils;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @program: autocode
 * @description:
 * @author: zhu.xiaojie
 * @create: 2020-08-20 23:26
 */
public class TableInfo {

    private String tableName;


    /**
     * 对应在 java 应用中的名称
     */
    private String javaName;

    /**
     *
     */
    private String tableComment;

    private String sql;

    private List<ColumnInfo> columns;

    private String javaPO;

    public String getJavaPO() {
        if(columns != null && columns.size() > 0){
            StringBuffer sb = new StringBuffer();

            Set<String> imports = new HashSet<>();
            for (ColumnInfo column : columns) {
                if(!ObjectUtils.isEmpty(column.getJavaType().getImport())){
                    imports.add(column.getJavaType().getImport());
                }
            }

            for (String ipt : imports) {
                sb.append(ipt).append("\n");
            }
            sb.append("import lombok.Data;\n");
            sb.append("\n\n");

            sb.append("/**\n");
            sb.append(" * 表名: "+ this.getTableName() +"\n");
            sb.append(" * \n");
            sb.append(" */\n");
            sb.append("@Data\n");
            sb.append("public class ").append(this.getClassName()).append(" {\n");

            for (ColumnInfo column : columns) {
                sb.append("\n");
                sb.append("\n");

                JavaType javaType = column.getJavaType();

                sb.append("    /**\n");
                sb.append("     * ").append(column.getColumnComment()).append("\n");
                sb.append("     */\n");
                sb.append("    private ").append(javaType.getName()).append(" ").append(JavaUtils.toJavaFieldName(column.getColumnName())).append(";");
            }

            sb.append("\n\n}");

            return sb.toString();
        }

        return null;
    }


    public String getClassName(){
        return JavaUtils.toJavaClassName(getJavaName() + "PO");
    }

    public String getJavaName() {
        if(this.javaName != null){
            return this.javaName;
        }else{
            return this.tableName;
        }
    }

    public void setJavaName(String javaName) {
        this.javaName = javaName;
    }




    public String getTableName() {
        return tableName;
    }

    public void setTableName(String tableName) {
        this.tableName = tableName;
    }

    public String getTableComment() {
        return tableComment;
    }

    public String getSql() {
        return sql;
    }

    public void setSql(String sql) {
        this.sql = sql;
    }

    public void setTableComment(String tableComment) {
        this.tableComment = tableComment;
    }

    public List<ColumnInfo> getColumns() {
        return columns;
    }

    public void setColumns(List<ColumnInfo> columns) {
        this.columns = columns;
    }


}
